package com.example.demo.service.Impl;

import com.example.demo.entity.Hotel;
import com.example.demo.entity.Hotel;
import com.example.demo.entity.HotelRecord;
import com.example.demo.mapper.HotelMapper;
import com.example.demo.mapper.HotelRecordMapper;
import com.example.demo.service.HotelService;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: demo
 * @description:
 * @author: Lelouch
 * @create: 2024-09-05 12:15
 **/
@Service
public class HotelServiceImpl implements HotelService {
    @Resource
    HotelMapper hotelMapper;
    @Resource
    HotelRecordMapper hotelRecordMapper;

    @Override
    public PageInfo<Hotel> list() {
        List<Hotel> hotels = hotelMapper.list();
        PageInfo<Hotel> hotelPageInfo = new PageInfo<>(hotels);
        return hotelPageInfo;
    }

    @Override
    public List<Hotel> search(String hotelname,String city) {
        Hotel hotel = new Hotel();
        hotel.setName(hotelname);
        hotel.setCity(city);
        return hotelMapper.search(hotel);
    }

    @Override
    public Integer currentNum(Integer hotelId) {
        List<HotelRecord> hotelRecords = hotelRecordMapper.selectByHotelId(hotelId);
        Integer cnt = 0;
        for (HotelRecord hotelRecord: hotelRecords) {
            if(hotelRecord.getEntry() == 1){
                cnt ++;
            }
            else{
                cnt --;
            }
        }
        return cnt;
    }
}
